//菜单
$(function () {
    var menu_dig = $("#menu_dig");
    var menu_dg = $('#menu_dg');
    var menu_form = $('#menu_form');
    var ObjectCmd ={
        /*保存*/
        saveUser:function () {
            menu_form.form("clear")

            menu_dig.dialog("setTitle", "add");
            menu_dig.dialog("open");

        },
        /*编辑*/
        editUser: function editUser() {
            var tr = menu_dg.datagrid("getSelected");

            if (!tr) {
                //没有选中
                $.messager.alert('温馨提示', "至少选中一条数据");
                return;
            }

            menu_form.form("clear")

            menu_dig.dialog("setTitle", "editUser");
            menu_dig.dialog("open");
            menu_form.form("load", tr)
        },
        /*删除*/
        deleteUser: function () {
            var tr = $('#menu_dg').datagrid("getSelected");
            if (!tr) {
                //没有选中
                $.messager.alert('温馨提示',"至少选中一条数据");
                return;
            }
            $.get("/menu/delete.do",{id:tr.id},function (data) {
                if (data.success) {
                    $.messager.alert('温馨提示','删除成功','info',function () {


                        $('#menu_dg').datagrid("reload");
                    });
                }
                else{
                    $.messager.alert('温馨提示',data.msg);


                }
            })
        },
        /*刷新*/
        reloadwin: function () {
            menu_dg.datagrid("reload");
        },

        /*关闭对话框*/
        closeWin:function () {
            menu_dig.dialog("close")

        },

        /*提交表单*/
        submitForm:function () {
            /*提交角色*/
            menu_form.form({
                url: '/menu/saveOrUpdate.do',
               
                success: function (data) {
                    var data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert('温馨提示', '保存成功', 'info', function () {

                            ObjectCmd.closeWin();
                            menu_dg.datagrid("reload");
                        });
                    }
                    else {
                        $.messager.alert('温馨提示', data.msg);
                        ObjectCmd.closeWin();

                    }
                }
            });


            menu_form.submit();
        }
    }

    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd")
        ObjectCmd[cmd]();
    })

    //编辑对话框
    menu_dig.dialog({
        width: 300,
        height: 200,
        closed: true,
        cache: false,
        modal: true,
        onClose:function () {

        },
        buttons: '#menu_dig_btn'
    })

    /*渲染菜单页面*/
    menu_dg.datagrid({
        url: '/menu/query.do',
        fitColumns: true,
        fit: true,
        pagination: true,
        singleSelect: true,
       toolbar: "#menu_tb",
        pageSize: 3,
        rownumbers: true,
        pageList: [3, 6, 9],
        onClickRow: function (index, row) {

        },
        columns: [[
            {field: 'text', title: '菜单名字', width: 100},
            {field: 'url', title: '菜单url', width: 100},


        ]]
    });

    $('#tt').tree({
        url:'/menu/getMenusNoSon.do',
        animate:true,
        onClick: function(node){
            console.log(node.id);
            $.get("/menu/getMenusByParentId.do",{id:node.id},function (data) {
                if (data) {
                    //返回子菜单显示到menu_dg里
                    menu_dg.datagrid("loadData",data)


                }
            })


        }

    });
})